import { IRootState } from "@/store/type";
import { Module } from "vuex";
import { ISystemState } from "./type";
import { ucfirst } from "@/utils/ucfirst";
import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData,
} from "@/service/main/system/system";

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuCount: 0,
      menuList: [],
    };
  },
  actions: {
    async getPageListAction(context, value) {
      const pageName = value.pageName;
      const pageUrl = `${pageName}/list`;
      const pageResult = await getPageListData(pageUrl, value.queryInfo);
      const { list, totalCount } = pageResult.data;
      context.commit(`change${ucfirst(pageName)}List`, list);
      context.commit(`change${ucfirst(pageName)}Count`, totalCount);
    },
    async deletePageDataAction(context, value) {
      //获取pageName和id
      const { pageName, id } = value;
      //拼接地址
      const pageUrl = `/${pageName}/${id}`;
      //调用删除请求
      await deletePageData(pageUrl);
      //重新请求最新的数据
      context.dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },
    async createPageDataAction(context, value) {
      const { pageName, newData } = value;
      const pageUrl = `/${pageName}`;
      await createPageData(pageUrl, newData);
      //重新请求最新的数据
      context.dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },
    async editPageDataAction(context, value) {
      const { pageName, editData, id } = value;
      const pageUrl = `/${pageName}/${id}`;
      console.log("editPageDataAction", pageUrl);
      await editPageData(pageUrl, editData);
      //重新请求最新的数据
      context.dispatch("getPageListAction", {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      });
    },
  },
  mutations: {
    changeUsersList(state: any, userList: any[]) {
      state.usersList = userList;
    },
    changeUsersCount(state: any, userCount: number) {
      state.usersCount = userCount;
    },
    changeRoleList(state: any, roleList) {
      state.roleList = roleList;
    },
    changeRoleCount(state: any, roleCount) {
      state.roleCount = roleCount;
    },
    changeGoodsList(state: any, goodsList) {
      state.goodsList = goodsList;
    },
    changeGoodsCount(state: any, goodsCount) {
      state.goodsCount = goodsCount;
    },
    changeMenuList(state: any, menuList) {
      state.menuList = menuList;
    },
    changeMenuCount(state: any, menuCount) {
      state.menuCount = menuCount;
    },
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
      };
    },
    pageListCount(state) {
      return (pageName: string) => {
        /* switch (pageName) {
          case "users":
            return state.usersCount;
          case "role":
            return state.roleCount;
        } */
        return (state as any)[`${pageName}Count`];
      };
    },
  },
};
export default systemModule;
